#include "qtreeitemdataproperty.h"

#include "../../libs/shared/qcommonstruct.h"
#include "../../libs/shared/qpropertyfactory.h"
#include "../../libs/shared/xmlobject.h"
#include "../../libs/shared/qtreeindexdata.h"

QTreeItemDataProperty::QTreeItemDataProperty(QAbstractProperty *parent):
    QAbstractProperty(parent)
{
    set_property("type","TreeIndex");
}

QIcon QTreeItemDataProperty::get_value_icon()
{
    return QIcon();
}

QString QTreeItemDataProperty::get_value_text()
{
    QTreeData info=get_value().value<QTreeData>();
    return QString("column:%1 row:%2").arg(info.m_labels.size()).arg(info.m_rows.size());
}

bool QTreeItemDataProperty::cmp(const QVariant &value)
{
    QTreeData info1=get_value().value<QTreeData>();
    QTreeData info2=value.value<QTreeData>();

    return info1==info2;
}

QString QTreeItemDataProperty::get_property_name()
{
    return "TreeIndex";
}

void QTreeItemDataProperty::toObject(XMLObject *xml)
{
    if(xml!=NULL)
    {
        xml->clear();
        xml->set_title(m_propertys.value("name").toString());

        QMap<QString,QVariant> pros=m_propertys;
        pros.remove("name");
        pros.remove("value");
        QMapIterator<QString,QVariant>      it(pros);

        while(it.hasNext())
        {
            it.next();
            xml->set_property(it.key(),it.value().toString());
        }

        QTreeData info=get_value().value<QTreeData>();
        info.to_xml(xml);
    }
}

void QTreeItemDataProperty::fromObject(XMLObject *xml)
{
    if(xml!=NULL)
    {
        QMapIterator<QString,QString>      it(xml->get_propertys());

        while(it.hasNext())
        {
            it.next();
            m_propertys.insert(it.key(),it.value());
        }
        QTreeData info;
        info.from_xml(xml);

        m_value.setValue<QTreeData>(info);
        refresh();

    }
}

qRegisterProperty(QTreeItemDataProperty)
